package com.fat.thegioivere.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.fat.thegioivere.R;
import com.fat.thegioivere.adapter.NewsListAdapter;
import com.fat.thegioivere.cache.TGVeReDataCache;
import com.fat.thegioivere.cache.TGVeReDataCache.OnDataChangedListener;
import com.fat.thegioivere.controller.DataRequestCallback;
import com.fat.thegioivere.model.NewsItem;
import com.fat.thegioivere.network.TGVeReRequests;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class NewsFragment extends LoadingStateFragment implements
		OnItemClickListener {

	private static final String TAG = "NewsFragment";

	private PullToRefreshListView mNewsListView;
	private NewsListAdapter mNewsListAdapter;

	/**
	 * Lưu trữ trạng thái có đang load dữ liệu hay không, tránh gọi load dữ liệu
	 * trùng lặp không cần thiết
	 * 
	 */
	private boolean mIsNewsListLoading = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_news, container, false);
		mNewsListView = (PullToRefreshListView) view
				.findViewById(R.id.listViewNews);

		mNewsListView.setOnItemClickListener(this);

		if (mNewsListAdapter != null) {
			mNewsListView.setAdapter(mNewsListAdapter);
		} else if (!mIsNewsListLoading) {
			// Đánh dấu là đã request dữ liệu rồi
			getNewslList();
		}

		TGVeReDataCache.getInstance(getActivity())
				.registerOnDataChangedListener(mDataSetChangedListener);

		return view;
	}

	private OnDataChangedListener mDataSetChangedListener = new OnDataChangedListener() {

		@Override
		public void onDataChanged(int type) {
			if (type == TYPE_TRAVEL && !mIsNewsListLoading) {
				getNewslList();
			}
		}
	};

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mNewsListView = null;
		TGVeReDataCache.getInstance(getActivity())
				.registerOnDataChangedListener(mDataSetChangedListener);

	}

	@Override
	public boolean isLoading() {
		return mIsNewsListLoading;
	}

	private void getNewslList() {
		mIsNewsListLoading = true;
		// gui request api de lay data
		TGVeReRequests.getBusinessList(getActivity(), false,
				mNewsListCallback);
	}

	private DataRequestCallback<ArrayList<NewsItem>> mNewsListCallback = new DataRequestCallback<ArrayList<NewsItem>>() {

		@Override
		public void onResult(final ArrayList<NewsItem> result,
				boolean continueWaiting) {
			Context c = getActivity();
			if (c == null) {
				return;
			}

			if (result != null && !result.isEmpty()) {
				mNewsListAdapter = new NewsListAdapter(c, result);
				if (mNewsListAdapter != null) {
					mNewsListView.setAdapter(mNewsListAdapter);
				}
				Toast.makeText(c, result.size() + " item", Toast.LENGTH_LONG)
						.show();
			} else {
				if (!continueWaiting) {
					// Có lỗi xảy ra khi request dữ liệu
					Toast.makeText(c, R.string.loading_error_toast,
							Toast.LENGTH_LONG).show();
				}
			}
			// Cập nhật trạng thái load dữ liệu
			mIsNewsListLoading = continueWaiting;
		}
	};

	@Override
	public void onItemClick(AdapterView<?> av, View v, int position, long id) {

	}

	public void onRefresh(final PullToRefreshBase<ListView> refreshView) {
		getNewslList();
	}
}
